package com.example.fw;

import org.openqa.selenium.By;
import com.example.tests.ContactObject;

public class ContactHelper extends HelperWithWebDriverBase {
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	public void createNewContact(ContactObject contactObject) {
    	 manager.getNavigationHelper().openMainPage();
    	 manager.getNavigationHelper().openNewContactPage();
    	 fillContactForm(contactObject, true);
    	 submitContactCreation();
     }

	public void modifyContact(int i, ContactObject contactObject) {
		 manager.getNavigationHelper().openMainPage();
		 initContactModification(i);
		 fillContactForm(contactObject, false);
		 submitContactModification();
		 manager.getNavigationHelper().openMainPage();
	}

	public void deleteContact(int i) {
		 manager.getNavigationHelper().openMainPage();
		 initContactModification(i);
		 submitContactDeletion();
		 manager.getNavigationHelper().openMainPage();
	}
	
	protected void fillContactForm(ContactObject contactObject, boolean fillGroup) {
		type(By.name("firstname"), contactObject.firstname);
		type(By.name("lastname"), contactObject.lastname);
		type(By.name("address"), contactObject.address);
		type(By.name("home"), contactObject.home);
		type(By.name("mobile"), contactObject.mobile);
		type(By.name("work"), contactObject.work);
		type(By.name("email"), contactObject.email);
		type(By.name("email2"), contactObject.email2);
		type(By.name("address2"), contactObject.address2);
		type(By.name("phone2"), contactObject.phone2);
		type(By.name("byear"), contactObject.byear);	
		select(By.name("bday"), contactObject.bday);
		select(By.name("bmonth"), contactObject.bmonth);
		if (fillGroup)
		{
			select(By.name("new_group"), contactObject.new_group);
		} 
		
	}

    protected void submitContactCreation() {
    	 click(By.name("submit"));
     }

    
	private void initContactModification(int i) {
		By initContactModificationButton = By.cssSelector("img[alt=\"Edit\"]");
		click(initContactModificationButton);
	}

	private void submitContactModification() {
		By submitContactModificationButton = By.xpath("/html/body/div/div[4]/form/input[11]");
		click(submitContactModificationButton);
		
		//click(By.value("update"));
		//driver.findElement(By.("update")).click();
	}

	private void submitContactDeletion() {
		By submitContactDeletionButton = By.xpath("/html/body/div/div[4]/form[2]/input[2]");
		click(submitContactDeletionButton);
		
		//click(By.value("delete"));
		//driver.findElement(By.name("delete")).click();
	}
	
		
}
